package com.ruoyi.project.system.controller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.system.domain.SysAccounts;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.service.ISysAccountsService;
import com.ruoyi.project.system.service.ISysConfigService;
import com.ruoyi.project.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysCash;
import com.ruoyi.project.system.service.ISysCashService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 公司申请提现Controller
 *
 * @author jjp
 * @date 2020-04-26
 */
@RestController
@Api("公司申请提现")
@RequestMapping("/system/cash")
public class SysCashController extends BaseController {
    @Autowired
    private ISysCashService sysCashService;

    @Autowired
    private ISysAccountsService sysAccountsService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询公司申请提现列表
     */
    @PreAuthorize("@ss.hasPermi('system:cash:list')")
    @ApiOperation("查询公司申请提现列表")
    @GetMapping("/list")
    public TableDataInfo list(SysCash sysCash) {
        startPage();
        List<SysCash> list = sysCashService.selectSysCashList(sysCash);
        return getDataTable(list);
    }

    /**
     * 获取公司申请提现详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:cash:query')")
    @ApiOperation("获取公司申请提现详细信息")
    @GetMapping(value = "/{cashId}")
    public AjaxResult getInfo(@PathVariable("cashId") Long cashId) {
        return AjaxResult.success(sysCashService.selectSysCashById(cashId));
    }

    /**
     * 修改公司申请提现
     */
    @PreAuthorize("@ss.hasPermi('system:cash:edit')")
    @ApiOperation("修改公司申请提现")
    @Log(title = "公司申请提现", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCash sysCash) {
        return toAjax(sysCashService.updateSysCash(sysCash));
    }


    /**
     * 新增公司申请提现
     */
    @ApiOperation("新增公司申请提现")
    @Log(title = "公司申请提现", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCash sysCash) {
        String value = (String) sysConfigService.selectConfigByKey("CONFIG_DEPTO_RATIO");
        Double exchange = Double.valueOf(value);
        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        //用户余额money1
        Double money1 = sysDept.getMoney();
        if (money1 > sysCash.getMoney()) {
            BigDecimal money = BigDecimal.valueOf(sysCash.getMoney());
            double v = money.multiply(BigDecimal.valueOf(exchange)).doubleValue();
            sysCash.setFactMoney(sysCash.getMoney() - v);
            sysCash.setDeptId(deptId);
            int i = sysCashService.insertSysCash(sysCash);
            sysDept.setMoney(money1 - sysCash.getMoney());
            sysCash.setDeptId(deptId);
            sysDeptService.updateDeptMoney(sysDept);
            if (i != 0) {
                SysAccounts sysAccounts = new SysAccounts();
                sysAccounts.setDeptId(sysCash.getDeptId());
                sysAccounts.setMoney(sysCash.getMoney());
                sysAccounts.setMoneyType("1");
                sysAccountsService.insertSysAccounts(sysAccounts);
            }
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
